<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.js"></script>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css" rel="stylesheet">
    <script>
        $(function () {
            // Handler for .ready() called.
            $("#submitBtn").on("click", function () {
                var email = $("#reg-email").val();
                var password = $("#reg-password").val();
                var code = $("#reg-code").val();
                $.ajax({
                    //url: "http://www.test.com/api/user/register/"+email+"/"+password+"/"+code,
                    //url: "http://localhost:8080/api/user/register/"+email+"/"+password+"/"+code,
                    //url: "http://192.168.20.1:9002/api/user/register/"+email+"/"+password+"/"+code,
                    url: "http://192.168.44.128:18088/api/user/register/"+email+"/"+password+"/"+code,
                    type: "POST",
					dataType: "json",
                    success: function (data) {
						console.log(data);
                        if(data.code == 0){
                            alert("注册成功");
                            location.href = "login.html";
                        }else if(data.code == 2){
                            alert("注册码已过期，请重新获取");
                        }else{
                            alert("注册失败，重新尝试");
                        }
                    },
                    error:function (data) {
						console.log("====error...");
						console.log(data);
                        if(data.status == 403){
                            alert("当前ip注册次数达到上限，禁止注册");
                        }
                    }
                })
            });

            var forbidRepeartClick = (function(){
                var instance = null;
                var canClick = true;
                function Build(){
                    this.setClick = function(callback){
                        if(canClick){
                            canClick = false;
                            callback()
                            setTimeout(function(){
                                canClick = true;
                            },60000)
                        }else{
                            console.log('一分钟之内限制重复点击')
                            alert("一分钟内请勿重发");
                        }
                    }
                }
                if(!instance){
                    instance = new Build();
                    console.log('创建实例')
                }
                return instance;
            })();

            $("#getCodeBtn").click(function(){
                forbidRepeartClick.setClick(function(){
                    var email = $("#reg-email").val();
                    $.ajax({
                        //url: "http://www.test.com/api/code/create/"+email,
                        //url: "http://localhost:8081/api/code/create/"+email,
                        //url: "http://192.168.20.1:9002/api/code/create/"+email,
                        url: "http://192.168.44.128:18088/api/code/create/"+email,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            if(data.code == 0){
                                alert("请到邮箱查看验证码！");
                            }else{
                                alert("这个邮箱已经注册过了");
                            }
                        },
                        error:function (data) {
                            console.log("====error...");
                            console.log(data);
                        }
                    })
                })
            });
        });
    </script>
    <title>Hello, world!</title>
</head>
<body>
<div class="container">
    <!-- Content here -->
    <form>
        <div class="form-group">
            <label for="reg-email">Email address</label>
            <input type="email" class="form-control" id="reg-email" aria-describedby="emailHelp">
            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
            <label for="reg-password">Password</label>
            <input type="password" class="form-control" id="reg-password">
        </div>
        <div class="form-group">
            <label for="reg-code">Code</label>
            <input type="text" class="form-control" id="reg-code">
            <button type="button" id="getCodeBtn" class="btn btn-primary">获取验证码</button>
        </div>
        <button type="button" id="submitBtn" class="btn btn-primary">注册</button>
    </form>
</div>
</body>
</html>